package com.sservice.banfu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sservice.banfu.e.AuctionRegister;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.mapper.AuctionRegisterMapper;
import com.sservice.banfu.mapper.SalePersonMapper;
import com.sservice.banfu.service.AuctionRegisterService;
import com.sservice.commonBusiness.service.PLetterService;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.AuctionRegisterVo;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.base.utils.service.SService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/25.
 */
@Controller
@RequestMapping("/auctionRegister/")
public class AuctionRegister1Controller {

    @Autowired
    AuctionRegisterService auctionRegisterService;
    @Autowired
    AuctionRegisterMapper auctionRegisterMapper;
    @Autowired
    SalePersonMapper salePersonMapper;
    @Autowired
    SalePersonService1 salePersonService1;
    @Autowired
    PLetterService pLetterService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(AuctionRegister auctionRegister, HttpSession session) {
        SalePerson salePerson = (SalePerson) session.getAttribute(SService.CURRENT_SP);
        if (salePerson != null) {
            auctionRegister.setOwnerId(String.valueOf(salePerson.getId()));
        }
        auctionRegister.setCreateAt(new Date());
        auctionRegisterMapper.insertSelective(auctionRegister);

        salePerson.setLevel("1"); // 已团队报名
        salePersonMapper.updateByPrimaryKeySelective(salePerson);
        session.setAttribute(SService.CURRENT_SP, salePerson);

        return new BaseResult(true, String.valueOf(auctionRegister.getId()), 200);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(@RequestParam Integer id) {
        return new BaseResult(true, String.valueOf(auctionRegisterService.deleteById(id)), 200);
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(AuctionRegister auctionRegister) {
        if ((new Integer(3)).equals(auctionRegister.getStatus())) {
            AuctionRegister ar = auctionRegisterService.selectById(auctionRegister.getId());
            if ((new Integer(1)).equals(ar.getStatus())) {
                return new BaseResult(false, "非法操作", 403);
            }
            pLetterService.toMsg("竞拍报名提交审核！团队名称：" + ar.getTeamName(),
                    ar.getOwnerId(), "1", "[竞拍报名申请]");
        } else if (!(new Integer(1)).equals(auctionRegister.getStatus())) {
            auctionRegister.setStatus(null);
        }
        return new BaseResult(true, String.valueOf(auctionRegisterService.updateByPrimaryKeySelective(auctionRegister)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(AuctionRegisterVo auctionRegister1) {
        AuctionRegisterVo auctionRegister = (AuctionRegisterVo) auctionRegisterService.selectById(auctionRegister1.getId());
        SalePersonVo salePersonVo = new SalePersonVo();
        salePersonVo.setType1(4); // 设置为4 报名成员
        List<SalePersonVo> list = salePersonService1.page(salePersonVo);
        auctionRegister.setList(list);
        return new BaseResult(true, auctionRegister);
    }

    @RequestMapping("get/{id}")
    @ResponseBody
    public BaseResult get(AuctionRegister auctionRegister1) {
        AuctionRegister auctionRegister = auctionRegisterService.selectById(auctionRegister1.getId());
        return new BaseResult(true, auctionRegister);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<AuctionRegister> list = auctionRegisterService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ListResult(true, pageInfo);
    }

    @RequestMapping("query")
    @ResponseBody
    public ListResult listCommon(AuctionRegister ar, HttpSession session) {
        if (ar.getOwnerId() != null && ar.getOwnerId().equals("MY")) {
            SalePerson salePerson = (SalePerson) session.getAttribute(SService.CURRENT_SP);
            ar.setOwnerId(String.valueOf(salePerson.getId()));
        }
        List<AuctionRegister> list = auctionRegisterService.query(ar);
        Pagination pagination = new Pagination();
        pagination.setTotal(auctionRegisterService.count(ar));
        ListResult lr = new ListResult(true, list);
        lr.setPage(pagination);
        return lr;
    }


}
